package edu.uclm.esi.tysw.podtube.domain;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import edu.uclm.esi.tysw.podtube.exceptions.UserAlreadyExistingException;
import edu.uclm.esi.tysw.podtube.exceptions.ErrorDeAccesoException;
import edu.uclm.esi.tysw.podtube.persistence.Conexion;
import edu.uclm.esi.tysw.podtube.persistence.UsuarioDAO;

public class Usuario {
	private int id;
	private String email;
	private Conexion bd;

	public Usuario(String email) {
		this.email = email;
	}

	public Usuario(String email, String pwd) throws ErrorDeAccesoException, SQLException {
		this.bd = UsuarioDAO.conectar(email, pwd);
		this.email = email;
		
		PreparedStatement ps = bd.prepareStatement("SELECT idUsuario FROM Usuarios WHERE email = ? ;");
		ps.setString(1, email);
		ResultSet rs = ps.executeQuery();
		rs.first();
		this.id = rs.getInt(1);
		bd.close();
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void insert(String pwd) throws UserAlreadyExistingException, SQLException {
		UsuarioDAO.insert(this, pwd);
	}

	public void delete() throws SQLException {
		UsuarioDAO.delete(this);
	}

	public int getId() {
		return id;
	}
}
